package hc;

import hc.util.UILang;

import java.awt.ComponentOrientation;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JDialog;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class JPTrayIcon extends TrayIcon {
	private HashMap<String, String> tip = new HashMap<String, String>();
	
	public static final String NAT_DESC = "Nat_Desc";
	public static final String PUBLIC_IP = "Pub_IP";
//	public static final String PUBLIC_PORT = "Pub_Port";
	
	private transient JPopupMenu menu;
	private transient final JDialog DIALOG = new JDialog((Frame) null);
	private transient final PopupMenuListener popupListener;
	private transient final MouseListener mouseListener;

	{
		DIALOG.setUndecorated(true);
		DIALOG.setAlwaysOnTop(true);
	}

	public void exit() {
		DIALOG.dispose();
	}
	
	public void putTip(String key, String value){
		tip.put(key, value);
		
		//重算ToolTip
		String toolTip = "";
		
		Iterator<String> it = tip.keySet().iterator();
		while(it.hasNext()){
			if(toolTip.length() > 0){
				toolTip += "\n";
			}
			toolTip += tip.get(it.next());
		}
		
		setToolTip(toolTip);
	}

	public JPTrayIcon(final Image image, String title, JPopupMenu menu) {
		super(image, title);
		setImageAutoSize(true);
		setImage(image);
		popupListener = new TrayPopupListener(DIALOG);
		mouseListener = new TrayMouseAdapter();
		this.setJPopupMenu(menu);
		
		setDefaultToolTip();
//		
//		Locale l = Locale.getDefault();
//		menu.applyComponentOrientation(ComponentOrientation.getOrientation(l));
	}

	public void setDefaultToolTip() {
		setToolTip("HomeCenter");
	}

	/**
	 * 
	 * @return
	 */
	public final JPopupMenu getJPopupMenu() {
		return menu;
	}

	/**
	 * 
	 * @param popmenu
	 */
	public final void setJPopupMenu(final JPopupMenu popmenu) {
		popmenu.applyComponentOrientation(ComponentOrientation.getOrientation(UILang.getUsedLocale()));
		
		if (this.menu != null) {
			this.menu.removePopupMenuListener(popupListener);
			removeMouseListener(mouseListener);
		}
		if (popmenu != null) {
			this.menu = popmenu;
			this.menu.addPopupMenuListener(popupListener);
			addMouseListener(mouseListener);
		}
	}

	private final class TrayMouseAdapter extends MouseAdapter {

		private void showJPopupMenu(final MouseEvent evt) {
			if (evt.isPopupTrigger() && menu != null) {
				DIALOG.setLocation(evt.getX(), evt.getY()
						- menu.getPreferredSize().height);
				DIALOG.setVisible(true);
				menu.show(DIALOG.getContentPane(), 0, 0);
				// popup works only for focused windows

				if (menu.getWidth() == 0) {
					menu.setVisible(true);
					menu.setVisible(false);
				}

				int w = menu.getWidth();
				int h = menu.getHeight();

				int sc_w = Toolkit.getDefaultToolkit().getScreenSize().width;
				int loc_x = evt.getX();
				if (loc_x + w > sc_w) {
					loc_x = loc_x - w;
				}

				menu.setLocation(loc_x, evt.getY() - h);
				menu.setVisible(true);
				menu.setInvoker(DIALOG.getContentPane());

				DIALOG.toFront();
			}
		}

		public void mousePressed(final MouseEvent evt) {
			showJPopupMenu(evt);
		}

		public void mouseReleased(final MouseEvent evt) {
			showJPopupMenu(evt);
		}

		public void mouseClicked(final MouseEvent evt) {
			showJPopupMenu(evt);
		}
	}

	private class TrayPopupListener implements PopupMenuListener {
		Dialog dialog;

		TrayPopupListener(Dialog dialog) {
			this.dialog = dialog;
		}

		@Override
		public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
			// not used
		}

		@Override
		public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
			//必须的，该逻辑是有用的。
			dialog.setVisible(false);
		}

		@Override
		public void popupMenuCanceled(final PopupMenuEvent evt) {
			//必须的，该逻辑是有用的。
			dialog.setVisible(false);
		}
	}
}
